package org.alvin.i18n.ui;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.Feature;
import com.alibaba.fastjson.serializer.SerializerFeature;

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.LinkedHashMap;

public class UniappI18nUtils {


    public static void main(String[] args) throws IOException {
        File jsonFile = new File("D:\\i18n.js");
        LinkedHashMap<String, String> resultJson = new LinkedHashMap<>();

        LinkedHashMap<String, Object> jsonObject = getJSONByJsFile(Paths.get(jsonFile.getAbsolutePath()));
        for (String key : jsonObject.keySet()) {
            JSONObject tmpJson = (JSONObject) jsonObject.get(key);
            LinkedHashMap<String, String> dataItem = JSONObject.parseObject(tmpJson.toString(), LinkedHashMap.class, Feature.OrderedField);
            for (String dataItemKey : dataItem.keySet()) {
                resultJson.put(key.concat(".").concat(dataItemKey), dataItem.get(dataItemKey));
            }
        }

        //格式化输出
        Path path = Paths.get("D:", jsonFile.getName()+"_001");
//            outData(path.toFile().getAbsolutePath(), JSON.toJSONString(map, SerializerFeature.PrettyFormat, SerializerFeature.MapSortField));
        String outString = JSON.toJSONString(resultJson, SerializerFeature.PrettyFormat, SerializerFeature.MapSortField);
        Files.write(path, outString.getBytes(StandardCharsets.UTF_8));

    }

//    public static void main(String[] args) throws IOException {
//        File file = new File("D:\\tangzhichao\\workspace\\xiaolan\\xl_platform_h5\\xl_platform_v3\\locale");
//        for (File dir : file.listFiles()) {
//            System.out.println(dir);
//            if (!dir.isDirectory()) {
//                continue;
//            }
//            String jsonName = "xl_" + dir.getName() + ".json";
//            LinkedHashMap<String, String> resultJson = new LinkedHashMap<>();
//            for (File jsonFile : dir.listFiles()) {
//                if ("index.ts".equals(jsonFile.getName().toLowerCase())) {
//                    continue;
//                }
//                LinkedHashMap<String, Object> jsonObject = getJSONByJsFile(Paths.get(jsonFile.getAbsolutePath()));
//                for (String key : jsonObject.keySet()) {
//                    JSONObject tmpJson = (JSONObject) jsonObject.get(key);
//                    LinkedHashMap<String, String> dataItem = JSONObject.parseObject(tmpJson.toString(), LinkedHashMap.class, Feature.OrderedField);
//                    for (String dataItemKey : dataItem.keySet()) {
//                        resultJson.put(key.concat(".").concat(dataItemKey), dataItem.get(dataItemKey));
//                    }
//                }
//            }
//            //格式化输出
//            Path path = Paths.get("D:", jsonName);
////            outData(path.toFile().getAbsolutePath(), JSON.toJSONString(map, SerializerFeature.PrettyFormat, SerializerFeature.MapSortField));
//            String outString = JSON.toJSONString(resultJson, SerializerFeature.PrettyFormat, SerializerFeature.MapSortField);
//            Files.write(path, outString.getBytes(StandardCharsets.UTF_8));
//        }
//    }


    public static void outData(String path, JSONObject jsonObject) throws IOException {
        System.out.println("输出结果" + path);
        Path realPath = Paths.get(path);
        String outString = JSON.toJSONString(jsonObject, SerializerFeature.PrettyFormat, SerializerFeature.WriteMapNullValue,
                SerializerFeature.WriteDateUseDateFormat);
        Files.write(realPath, outString.getBytes(StandardCharsets.UTF_8));
    }

    private static LinkedHashMap<String, Object> getJSONByJsFile(Path path) throws IOException {
        byte[] buff1 = Files.readAllBytes(path);
        String content = new String(buff1, StandardCharsets.UTF_8);
        int index1 = content.lastIndexOf("export default {");
        if(index1 != -1) {
            int lastIndex = content.lastIndexOf("}");
            content = content.substring(index1 + "export default {".length() - 1, lastIndex + 1);
            //
        }
        System.out.println("--读取内容路径：" + path);
        return JSONObject.parseObject(content, LinkedHashMap.class, Feature.OrderedField);
    }
}
